/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.c0910g.Servlet;

import com.google.gson.Gson;
import com.myapp.c0910g.DAO.PaymentDetailsDAO;
import com.myapp.c0910g.DAO.PaymentTypeDAO;
import com.myapp.c0910g.DAO.UserRadioCabDAO;
import com.myapp.c0910g.DTO.UserRadioDTO;
import com.myapp.c0910g.Utility.Constant;
import com.myapp.c0910g.entity.PaymentDetails;
import com.myapp.c0910g.entity.PaymentType;
import com.myapp.c0910g.entity.UserRadioCab;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author BachLe
 */
public class LoginServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        UserRadioCabDAO userDAO = new UserRadioCabDAO();
        PaymentDetailsDAO payDAO = new PaymentDetailsDAO();
        PaymentTypeDAO paytypeDAO = new PaymentTypeDAO();
        try {
            String loginType = (String)request.getParameter(Constant.LOGIN_TYPE);

            if(loginType.equals(Constant.ROLE_ADMIN)){
                String username = (String)request.getParameter(Constant.USERNAME);
                String password = (String)request.getParameter(Constant.PASSWORD);

                UserRadioCab user = userDAO.loginAction(loginType, username, password);
                if(user != null){
                    UserRadioDTO dto = new UserRadioDTO();
                    dto.setuRadioId(user.getUrcid());
                    dto.setuRadioName(user.getURCName());
                    dto.setuRadioAddress(null);
                    dto.setuRadioStartDate(null);
                    dto.setuRadioEndDate(null);
                    dto.setuRadioPaymentType(null);
                    dto.setuRadioPaymentBy(null);

                    HttpSession session = request.getSession();
                    session.setAttribute(Constant.DATAINFOR, dto);
                    out.println("<result>success</result>");
                }else{
                    out.println("<result>error</result>");
                }
            }

            if(loginType.equals(Constant.USER_LISTING)){
                String username = (String)request.getParameter(Constant.USERNAME);
                String password = (String)request.getParameter(Constant.PASSWORD);

                UserRadioCab user = userDAO.loginAction(loginType, username, password);
                if(user != null){
                    UserRadioDTO dto = new UserRadioDTO();
                    PaymentDetails paydetail = payDAO.findPayDetailByUserId(user.getUrcid());
                    PaymentType paytype = paytypeDAO.findPaymentTypeById(user.getPaymentType().getPaymentTypeID());
                    
                    dto.setuRadioId(user.getUrcid());
                    dto.setuRadioName(user.getURCName());
                    dto.setuRadioAddress(user.getURCAddress());
                    dto.setuRadioStartDate(paydetail.getPDStartDate());
                    dto.setuRadioEndDate(paydetail.getPDEndDate());
                    dto.setuRadioPaymentType(paytype.getPaymentTypeName());
                    dto.setuRadioPaymentBy(paytype.getPaymentBy());
                    HttpSession session = request.getSession();
                    session.setAttribute(Constant.DATAINFOR, dto);
                    //request.getRequestDispatcher("/RadioCabsProject-war/Home/home.jsp");
                    out.println("<result>success</result>");
                }else{
                    out.println("<result>error</result>");
                }
            }

            if(loginType.equals(Constant.USER_ADVERTISE)){
                String username = (String)request.getParameter(Constant.USERNAME);

                UserRadioCab user = userDAO.loginActionForAdvertise(loginType, username);
                if(user != null){
                    UserRadioDTO dto = new UserRadioDTO();
                    PaymentDetails paydetail = payDAO.findPayDetailByUserId(user.getUrcid());
                    PaymentType paytype = paytypeDAO.findPaymentTypeById(user.getPaymentType().getPaymentTypeID());
                    dto.setuRadioId(user.getUrcid());
                    dto.setuRadioName(user.getURCName());
                    dto.setuRadioAddress(user.getURCAddress());
                    dto.setuRadioStartDate(paydetail.getPDStartDate());
                    dto.setuRadioEndDate(paydetail.getPDEndDate());
                    dto.setuRadioPaymentType(paytype.getPaymentTypeName());
                    dto.setuRadioPaymentBy(paytype.getPaymentBy());
                    
                    HttpSession session = request.getSession();
                    session.setAttribute(Constant.DATAINFOR, dto);
                    //request.getRequestDispatcher("/RadioCabsProject-war/Home/home.jsp");
                    out.println("<result>success</result>");
                }else{
                    out.println("<result>error</result>");
                }
            }

            if(loginType.equals(Constant.USER_DRIVER)){
                String username = (String)request.getParameter(Constant.USERNAME);
                String password = (String)request.getParameter(Constant.PASSWORD);

                UserRadioCab user = userDAO.loginAction(loginType, username, password);
                if(user != null){
                    UserRadioDTO dto = new UserRadioDTO();
                    PaymentDetails paydetail = payDAO.findPayDetailByUserId(user.getUrcid());
                    PaymentType paytype = paytypeDAO.findPaymentTypeById(user.getPaymentType().getPaymentTypeID());
                    dto.setuRadioId(user.getUrcid());
                    dto.setuRadioName(user.getURCName());
                    dto.setuRadioAddress(user.getURCAddress());
                    dto.setuRadioStartDate(paydetail.getPDStartDate());
                    dto.setuRadioEndDate(paydetail.getPDEndDate());
                    dto.setuRadioPaymentType(paytype.getPaymentTypeName());
                    dto.setuRadioPaymentBy(paytype.getPaymentBy());

                    HttpSession session = request.getSession();
                    session.setAttribute(Constant.DATAINFOR, dto);
                    //request.getRequestDispatcher("/RadioCabsProject-war/Home/home.jsp");
                    out.println("<result>success</result>");
                }else{
                    out.println("<result>error</result>");
                }
            }
        }catch(Exception ex){
            out.print("<result>error</result>");
            ex.printStackTrace();
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
